package com.lkei.controller;

import com.alibaba.fastjson.JSONObject;
import com.lkei.dto.Result;
import com.lkei.entity.ClassMessage;
import com.lkei.entity.TeacherStudentNotice;
import com.lkei.entity.TeacherStudentTask;
import com.lkei.service.*;
import com.lkei.service.ClassMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("teacherMessage")
public class TeacherMessageController {
    @Autowired
    private TeacherMessageTaskService teacherMessageTaskService;

    @Autowired
    private TeacherMessageNoticeService teacherMessageNoticeService;

    @Autowired
    private ClassMessageService classMessageService;

    @Autowired
    private TheClassService theClassService;

    @Autowired
    private ResultService resultService;

    @CrossOrigin
    @RequestMapping("addMessage")
    public Result addMessage(@RequestBody String data){

        //获取当前的日期,为任务存储路径添加唯一标识
        Date date = new Date();
        //设置时间格式
        SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH时mm分ss秒");
        //获取String类型的时间
        String createdate = sdf.format(date);

        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        // 取出map中的data,继续进行Json解析
        JSONObject jsonObject2 = JSONObject.parseObject(map.get("data").toString());
        Map<String, Object> map2 = new HashMap<>();
        map2.putAll(jsonObject2);
        //取出班级号
        List<Integer> classes_id = (List<Integer>) map2.get("class");


        // 如果消息类型是任务
        if ("task".equals(map2.get("message_type").toString())){

            TeacherStudentTask teacherStudentTask = new TeacherStudentTask();
            teacherStudentTask.setMessage_title(map2.get("message_title").toString());
            teacherStudentTask.setMessage_detail(map2.get("message_detail").toString());
            teacherStudentTask.setResult_name(map2.get("task_result").toString());


            // 路径生成规则： D:/教职工号/任务结果名 时间
            String resultURL ="/www/项目任务结果存储文件/"
                    +map.get("account").toString()
                    + "/" + map2.get("task_result").toString() +createdate;
            teacherStudentTask.setResult_url(resultURL);
            teacherStudentTask.setPublisher_id(Integer.valueOf(map.get("account").toString()).intValue());
            teacherStudentTask.setTime(map2.get("time").toString());
            // 插入（教师-学生）任务信息
            teacherMessageTaskService.addTeacherTask(teacherStudentTask);

            List<ClassMessage> classMessages = new ArrayList<>();
            for (Integer class_id: classes_id) {
                ClassMessage classMessage = new ClassMessage();
                classMessage.setMessage_id(teacherStudentTask.getId());
                classMessage.setClass_id(class_id);
                classMessages.add(classMessage);
            }
            // 插入信息到班级消息表
            classMessageService.addClassMessage(classMessages);


        }
        // 如果消息类型是通知
        if("notice".equals(map2.get("message_type").toString())){

            TeacherStudentNotice teacherStudentNotice = new TeacherStudentNotice();
            teacherStudentNotice.setMessage_title(map2.get("message_title").toString());
            teacherStudentNotice.setMessage_detail(map2.get("message_detail").toString());
            teacherStudentNotice.setPublisher_id(Integer.valueOf(map.get("account").toString()).intValue());
            teacherStudentNotice.setTime(map2.get("time").toString());
            //插入（教师-学生）通知信息
            teacherMessageNoticeService.addTeacherNotice(teacherStudentNotice);

            List<ClassMessage> classMessages2 = new ArrayList<>();
            for (Integer class_id: classes_id) {
                ClassMessage classMessage = new ClassMessage();
                classMessage.setMessage_id(teacherStudentNotice.getId());
                classMessage.setClass_id(class_id);
                classMessages2.add(classMessage);
            }
            // 插入信息到班级消息表
            classMessageService.addClassNotice(classMessages2);
        }


        return Result.INSERT_SUCCESS;
    }

    @CrossOrigin
    @RequestMapping("findTask")
    public Result findTask(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int teacherID = Integer.valueOf(map.get("account").toString()).intValue();
        List<TeacherStudentTask> tasks = teacherMessageTaskService.findTask(teacherID);
        return Result.SUCCESS.setNewData(tasks);
    }

    @CrossOrigin
    @RequestMapping("findNotice")
    public Result findNotice(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int teacherID = Integer.valueOf(map.get("account").toString()).intValue();
        List<TeacherStudentNotice> notices = teacherMessageNoticeService.findNotice(teacherID);
        return Result.SUCCESS.setNewData(notices);
    }

    @CrossOrigin
    @RequestMapping("findTaskDetail")
    public Result findTaskDetail(@RequestBody String data){
        System.out.println(data);
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int MessageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        List<TeacherStudentTask> taskByMessageID = teacherMessageTaskService.findTaskByMessageID(MessageID);

        return Result.SUCCESS.setNewData(taskByMessageID);
    }

    @CrossOrigin
    @RequestMapping("findNoticeDetail")
    public Result findNoticeDetail(@RequestBody String data){

        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int MessageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        String message_detail = teacherMessageNoticeService.findNoticeByMessageID(MessageID);
        return Result.SUCCESS.setNewData(message_detail);
    }

    /**
     * 根据消息编号查询消息关系
     * @param data
     * @return
     */
    @CrossOrigin
    @RequestMapping("findByMessageID")
    public Result findByMessageID(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int MessageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        List<String> class_name = classMessageService.findByMessageID(MessageID);
        return Result.SUCCESS.setNewData(class_name);
    }

    /**
     * 根据消息编号查询消息关系
     * @param data
     * @return
     */
    @CrossOrigin
    @RequestMapping("findNoticeClass")
    public Result findNoticeClass(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int MessageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        List<String> class_name = classMessageService.findNoticeClass(MessageID);
        return Result.SUCCESS.setNewData(class_name);
    }

    @CrossOrigin
    @RequestMapping("finishPercent")
    public Result finishPercent(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int MessageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        Integer studentNumber = theClassService.findStudentNumber(MessageID);
        Integer integer = resultService.finishNumber(MessageID);
        double a = (double)integer/studentNumber * 100;
        a = (double) Math.round(a * 100) / 100;
        return Result.SUCCESS.setNewData(a);
    }

    @CrossOrigin
    @RequestMapping("deleteMessage")
    public Result deleteMessage(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int MessageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        teacherMessageTaskService.DeleteMessage(MessageID);
        teacherMessageTaskService.DeleteRelationship(MessageID);
        return Result.DELETE_SUCCESS;
    }


    @CrossOrigin
    @RequestMapping("deleteNotice")
    public Result deleteNotice(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int MessageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        teacherMessageNoticeService.DeleteMessage(MessageID);
        teacherMessageNoticeService.DeleteRelationship(MessageID);
        return Result.DELETE_SUCCESS;
    }
}
